﻿/*******************************************************************************
 * Copyright © 2016-2020 北京网非科技有限公司 版权所有
 * Author: 北京网非科技有限公司
 * Description: 网非快速开发平台NET
 * Website：http://www.wfdot.com
*********************************************************************************/
using CubicFWNet.Data.SystemManage;
using System;
using System.Collections.Generic;
using CubicFWNet.Common;
using System.Linq;
using System.Threading.Tasks;
using CubicFWNet.Data.SystemSecurity;
using CubicFWNet.Data.SystemSecurity.Model;
using CubicFWNet.Data.SystemManage.Model;

namespace CubicFWNet.Application.SystemSecurity
{
    public class LogApp
    {
        private LogInLogRepository service = new LogInLogRepository();
        private UserRepository userRepository = new UserRepository();

        public IEnumerable<LogInLog> Get(string keyword, string timeType)
        {
            var expression = ExtLinq.True<LogInLog>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FamilyName.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(timeType))
            {
                DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
                DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
                switch (timeType)
                {
                    case "today":
                        break;
                    case "week":
                        startTime = DateTime.Now.AddDays(-7);
                        break;
                    case "month":
                        startTime = DateTime.Now.AddMonths(-1);
                        break;
                    case "3month":
                        startTime = DateTime.Now.AddMonths(-3);
                        break;
                    default:
                        break;
                }
                expression = expression.And(t => t.Time >= startTime && t.Time <= endTime);
            }
            return service.IQueryable(expression).OrderByDescending(a => a.Time);
        }
        public void RemoveLog(string keepTime)
        {
            DateTime operateTime = DateTime.Now;
            if (keepTime == "7")            //保留近一周
            {
                operateTime = DateTime.Now.AddDays(-7);
            }
            else if (keepTime == "1")       //保留近一个月
            {
                operateTime = DateTime.Now.AddMonths(-1);
            }
            else if (keepTime == "3")       //保留近三个月
            {
                operateTime = DateTime.Now.AddMonths(-3);
            }
            var expression = ExtLinq.True<LogInLog>();
            expression = expression.And(t => t.Time <= operateTime);
            service.Delete(expression);
        }

        /// <summary>
        /// 写日志 同步方法
        /// </summary>
        /// <param name="LogInLog"></param>
        public void WriteDbLog(LogInLog LogInLog)
        {
            LogInLog.Id = Guid.NewGuid();
            LogInLog.Time = DateTime.Now;
            LogInLog.IPAddress = Net.Ip;
            LogInLog.IPAddressName = Net.GetLocation(LogInLog.IPAddress);
            service.Insert(LogInLog);
        }

        /// <summary>
        /// 写日志 异步方法
        /// </summary>
        /// <param name="LogInLog"></param>
        public Task WriteDbLogAsync(LogInLog LogInLog)
        {
            return Task.Run(() =>
            {
                LogInLog.Id = Guid.NewGuid();
                LogInLog.Time = DateTime.Now;
                LogInLog.IPAddress = Net.Ip;
                LogInLog.IPAddressName = Net.GetLocation(LogInLog.IPAddress);
                service.Insert(LogInLog);
            });
        }

        /// <summary>
        /// 密码错误 更新用户状态
        /// </summary>
        /// <param name="id">用户Id</param>
        public void PasswordErrorUpdateUserStatus(Guid userId)
        {
            var date = DateTime.Today;
            var count = service.Count(a => a.UserId == userId && a.Type == "密码错误" && a.Time.Year == date.Year
                       && a.Time.Month == date.Month
                       && a.Time.Day == date.Day);
            if (count > 6)
            {
                userRepository.Update(a => a.Id == userId, a => new User{ Status = false });
            }
        }

    }
}
